import { defineStore } from "pinia";
import { getEquipmentDetail } from "../api/aiot/equipment";
import { ParEquipmentDetail } from "../api/aiot/equipment/models";
import { getProduct } from "../api/aiot/product";
import { ParProductItem } from "../api/aiot/product/models";
import { getProjectSummaryVo } from "/@/api/aiot/projectManage";
import { ReqProjectInfo } from "/@/api/aiot/projectManage/models";
import type { ConfigurationShowOptions } from "/@/types/components";

interface LoadingParams {
  loading: boolean;
  text?: string;
}

export const useHomeStore = defineStore("home", {
  state: (): {
    loading: boolean;
    loadingText: string;
    pageLoading: boolean;
    contentScrollable: boolean;
    configurationShowOptions: ConfigurationShowOptions;
    darkMode: "dark" | "light";
    sumUnreadCount: number;
    orderDetailVisible: boolean;
    orderId: string;
    //供NoticeOverview组件实现
    refreshUnread: () => void;
    curNoticeKey: string;
    //项目详情数据
    loadingProjectDetail: boolean;
    firstLoadingProjectDetail: boolean;
    projectDetail: ReqProjectInfo;
    projectErr: boolean;
    //设备详情
    equipmentDetail: ParEquipmentDetail;
    loadingEquipmentDetail: boolean;
    firstLoadingEquipmentDetail: boolean;
    equipmentErr: boolean;
    //产品详情
    productDetail: ParProductItem;
    loadingProductDetail: boolean;
    firstLoadingProductDetail: boolean;
    productErr: boolean;
  } => {
    return {
      loading: false,
      loadingText: "",
      pageLoading: false,
      contentScrollable: true,
      configurationShowOptions: { projectId: "" },
      darkMode: "light",
      sumUnreadCount: 0,
      orderDetailVisible: false,
      orderId: "",
      refreshUnread() {},
      curNoticeKey: "",
      projectDetail: {},
      loadingProjectDetail: false,
      firstLoadingProjectDetail: true,
      projectErr: false,
      equipmentDetail: {},
      firstLoadingEquipmentDetail: true,
      loadingEquipmentDetail: false,
      equipmentErr: false,
      productDetail: {},
      loadingProductDetail: false,
      firstLoadingProductDetail: true,
      productErr: false,
    };
  },
  actions: {
    toggleLoading(params: LoadingParams) {
      this.loading = params.loading;
      this.loadingText = params.text || "";
    },
    togglePageLoading(params: LoadingParams) {
      this.pageLoading = params.loading;
    },
    showOrderDetail(orderId: string) {
      this.orderId = orderId;
      this.orderDetailVisible = true;
    },
    async reloadProjectDetail(projectId: string) {
      this.projectErr = false;
      this.loadingProjectDetail = true;
      const data = await getProjectSummaryVo(
        {
          projectId,
        },
        false
      );
      this.loadingProjectDetail = false;
      this.firstLoadingProjectDetail = false;
      if (data) {
        this.projectDetail = data;
      } else {
        this.projectErr = true;
      }
    },
    async reloadEquipmentDetail(equipmentId: string) {
      this.equipmentErr = false;
      this.loadingEquipmentDetail = true;
      const data = await getEquipmentDetail(equipmentId);
      this.loadingEquipmentDetail = false;
      this.firstLoadingEquipmentDetail = false;
      if (data) {
        this.equipmentDetail = data;
      } else {
        this.equipmentErr = true;
      }
    },
    async reloadProductDetail(productId: string) {
      this.productErr = false;
      this.loadingProductDetail = true;
      const data = (await getProduct(productId)) || {};
      this.loadingProductDetail = false;
      this.firstLoadingProductDetail = false;
      if (data) {
        this.productDetail = data;
      } else {
        this.productErr = true;
      }
    },
  },
});
